import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import '../services/error_handler_service.dart';

enum ViewState {
  idle,
  busy,
  error,
  empty
}

class BaseViewModel extends ChangeNotifier {
  final _errorHandler = ErrorHandlerService();
  ViewState _state = ViewState.idle;
  String? _errorMessage;

  ViewState get state => _state;
  String? get errorMessage => _errorMessage;
  bool get isBusy => _state == ViewState.busy;

  void setState(ViewState viewState) {
    _state = viewState;
    notifyListeners();
  }

  void setError(String message, {BuildContext? context}) {
    _errorMessage = message;
    _state = ViewState.error;
    _errorHandler.handleError(context, message);
    notifyListeners();
  }

  void setBusy(bool value) {
    _state = value ? ViewState.busy : ViewState.idle;
    notifyListeners();
  }

  @override
  void dispose() {
    super.dispose();
  }
}